package com.asia5b.a5mall.middleware.utils;

/**
 * Created by linet on 2017/8/22.
 */

import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.asia5b.a5mall.middleware.R;
import com.asia5b.a5mall.middleware.base.BaseApplication;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

/**
 * Created by zhangyuncai on 2018/9/11.
 */
public final class WImageLoader {

    private WImageLoader() {
    }

    public static void displayImageWithThumbnail(String uri, ImageView imageView) {
        try {
            RequestOptions option = getOption(0, 0, 0);
            Glide.with(imageView)
                    .load(uri)
                    .apply(option)
                    .thumbnail(0.1f)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 默认加载图片的方法
     *
     * @param uri
     * @param imageView
     */
    public static void displayImage(String uri, ImageView imageView) {
        try {
            RequestOptions option = getOption(0, 0, 0);
            RequestOptions.bitmapTransform(new RoundedCorners(5));
            Glide.with(imageView)
                    .load(uri)
                    .apply(option)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 默认加载图片的方法
     *
     * @param uri
     * @param imageView
     */
    public static void displayRoundImage(String uri, ImageView imageView, int radius) {
        try {
            radius = DisplayUtils.dip2px(BaseApplication.sContext, radius);
            RequestOptions option = getOption(0, 0, 0);
            option.transforms(new CenterCrop(), new RoundedCorners(radius));
            Glide.with(imageView)
                    .load(uri)
                    .apply(option)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 默认加载图片的方法
     *
     * @param uri
     * @param imageView
     */
    public static void displayRoundImageOption(String uri, ImageView imageView, int radius) {
        try {
            radius = DisplayUtils.dip2px(BaseApplication.sContext, radius);
            RequestOptions option = getOption(0, 0, 0);
            option.transforms(new FitCenter(), new RoundedCorners(radius));
            Glide.with(imageView)
                    .load(uri)
                    .apply(option)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param uri
     * @param imageView
     * @param errorDrawableId 加载失败后的默认图片
     */
    public static void displayImage(String uri, ImageView imageView, @DrawableRes int errorDrawableId) {
        try {
            RequestOptions option = getOption(errorDrawableId, errorDrawableId, errorDrawableId);
            Glide.with(imageView)
                    .load(uri)
                    .apply(option)
                    .into(imageView);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 默认加载显示
     * 此方法d等ui确定后还需要添加默认的loading、error、null图片
     *
     * @return
     */
    private static RequestOptions getOption(@DrawableRes int loadingDrawable, @DrawableRes int errorDrawable, @DrawableRes int netDrawable) {
        RequestOptions options = new RequestOptions();
        options.centerCrop();
        if (loadingDrawable != 0) {
            options.placeholder(loadingDrawable);//加载中默认图片
        } else {
            options.placeholder(R.drawable.image_loading);
        }
        if (errorDrawable != 0) {
            options.error(errorDrawable);//  加载错误默认图片
        } else {
            options.error(R.drawable.image_error);
        }
        if (netDrawable != 0) {
            options.fallback(netDrawable);//请求url/model为空
        } else {
            options.fallback(R.drawable.img_default);
        }
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.skipMemoryCache(false);//跳过内存缓存

        return options;
    }


}
